package com.yjd.bi.report.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.yjd.bi.base.service.impl.BaseServiceImpl;
import com.yjd.bi.report.dao.IBiDataChangeRecordDao;
import com.yjd.bi.report.dao.IBiDataChangeRecordLogDao;
import com.yjd.bi.report.model.BiDataChangeRecordLogModel;
import com.yjd.bi.report.model.BiDataChangeRecordModel;
import com.yjd.bi.report.service.IBiDataChangeRecordService;
import com.yjd.bi.report.service.IBiDatabaseInfoService;
import com.yjd.bi.report.service.IBiEtlFromtableTotableService;
import com.yjd.bi.report.service.IBiTableInfoService;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.util.FrameUtil;

/**
 * <b>description</b>：数据变更记录业务实现<br>
 * <b>time</b>：2016-04-08 15:29:55 <br>
 * <b>author</b>： ready likun_557@163.com
 */
@Service("biDataChangeRecordService")
public class BiDataChangeRecordServiceImpl extends BaseServiceImpl implements
		IBiDataChangeRecordService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public BiDataChangeRecordModel insert(BiDataChangeRecordModel model)
			throws Exception {
		return this.biDataChangeRecordDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(BiDataChangeRecordModel model) throws Exception {
		return this.biDataChangeRecordDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.biDataChangeRecordDao.deleteModel(paramMap, true);
	}

	/**
	 * 判断数据是否存在
	 * 
	 * @param database_info_id
	 * @param data_change_record_id
	 * @param dbw
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public boolean isExists(long database_info_id, long data_change_record_id,
			DbWREnums dbw) throws Exception {
		Map<Object, Object> paramMap = FrameUtil.newHashMap("database_info_id",
				database_info_id, "data_change_record_id",
				data_change_record_id);
		return this.biDataChangeRecordDao.getModelListCount(paramMap,
				dbw.isWrite()) >= 1
				|| this.biDataChangeRecordLogDao.getModelListCount(paramMap,
						dbw.isWrite()) >= 1;
	}

	/**
	 * 将ids的数据移到日志表
	 * 
	 * @param ids
	 * @throws Exception
	 */
	public void moveDataToDataLog(List<Long> ids) throws Exception {
		if (ids != null && !ids.isEmpty()) {
			this.biDataChangeRecordLogDao.copyBiDataChangeRecordToLog(ids);
			this.biDataChangeRecordDao.deleteModel(
					FrameUtil.newHashMap("idList", ids), true);
		}
	}

	/**
	 * 将id的数据移到日志表
	 * 
	 * @param id
	 * @param isJcNotLastRow
	 *            是否需要检查不是是最后一条
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void moveDataToDataLog1(Long id, boolean isJcNotLastRow)
			throws Exception {
		if (id != null) {
			// 最后留一条，避免mysql重启之后自增从1开始
			if (isJcNotLastRow
					&& this.biDataChangeRecordDao.getModelListCount(
							FrameUtil.newHashMap("gt_id", id),
							DbWREnums.WRITE.isWrite()) == 0) {
				return;
			} else {
				BiDataChangeRecordModel model = this.biDataChangeRecordDao
						.getModelById(id, DbWREnums.WRITE.isWrite());
				if (model != null) {
					this.biDataChangeRecordDao.deleteById(id,
							DbWREnums.WRITE.isWrite());
					BiDataChangeRecordLogModel m = new BiDataChangeRecordLogModel();
					BeanUtils.copyProperties(model, m);
					this.biDataChangeRecordLogDao.insertModel(m,
							DbWREnums.WRITE.isWrite());
				}
			}

		}
	}

	@Resource
	private IBiDataChangeRecordDao biDataChangeRecordDao;
	@Resource
	private IBiDataChangeRecordLogDao biDataChangeRecordLogDao;
	@Resource
	private IBiDatabaseInfoService biDatabaseInfoService;
	@Resource
	private IBiEtlFromtableTotableService biEtlFromtableTotableService;
	@Resource
	private IBiTableInfoService biTableInfoService;

	@Override
	public IBaseDao getBaseDao() {
		return this.biDataChangeRecordDao;
	}

}
